// pages/order/order.js
const app = getApp();
Page({
  data: {
    order: [
      {
        src: "",
        name: "",
        status: "",
        goods: [{
          goods_img: "",
          info_title: "",
          norms: "",
          stock: "",
          price: "",
          quantity: 0,
        }],
        quantity: 0,
        total: "0"
      }
    ],
    xianshi:'true',
  },
  // 点击进入详情页
  goodsurl: function () {
    wx.navigateTo({
      url: '../goodsDetails/goodsDetails',
    })
  },
  onLoad: function (options) {
    
  },
  onReady: function () {

  },
  onShow: function () {
    var that = this;
    that.setData({ order: [] })
    wx.request({
      url: app.url + '/Order/order_list',
      method: 'post',
      data: { uid: wx.getStorageSync('uid') },
      success: function (res) {
        console.log(res)
        if (res.data.code == 1) {
          that.setData({ order: res.data.data })
        } else {
          that.setData({ order: [] })
          wx.showToast({
            title: '暂无订单'
          })
        }
      },
      error: function () { }
    })
  },
  onHide: function () {

  },
  onUnload: function () {

  },
  onPullDownRefresh: function () {

  },
  onReachBottom: function () {

  },
  onShareAppMessage: function () {

  },
  evaluate: function (e) {
    var oid = e.target.id;
    wx.navigateTo({
      url: '../evaluate/evaluate?oid=' + oid,
    })
  },
  delorder: function(e){
    var oid = e.target.id;
    var that=this;
    wx.request({
      url: app.url + '/order/delOrder',
      data: { 
        orderid: oid
      },
      method: 'post',
      success: function (res) {
        console.log(res);
        wx.showToast({
          title: '订单已取消'
        })
        wx.request({
          url: app.url + '/Order/order_list',
          method: 'post',
          data: { uid: wx.getStorageSync('uid') },
          success: function (res) {
            if (res.data.code == 1) {
              that.setData({ order: res.data.data })
            } else {
              that.setData({ order: [] })
              wx.showToast({
                title: '暂无订单'
              })
            }
          },
        })
      },
    })
  },
  wxpay:function(e){
    var order_sn = e.target.id;
    wx.request({
      url: app.url + '/Pay/wxpay',
      data: { order_sn: order_sn },
      method: 'post',
      success: function (res) {
        if (res) {
          wx.requestPayment({
            timeStamp: res.data.timeStamp,
            nonceStr: res.data.nonceStr,
            package: res.data.package,
            signType: res.data.signType,
            paySign: res.data.paySign,
            success: function (pay) {
              wx.showToast({ title: '支付成功', })
              wx.navigateTo({ url: '../order/order' })
            },
            fail: function () {
              wx.showToast({ title: '支付失败', })
              wx.navigateTo({ url: '../order/order' })
            },
            complete: function () {
              wx.navigateTo({ url: '../order/order' })
            }
          })
        }
      },
      error: function (err) { }
    })
  }
})